package com.fz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fz.entity.Cart;
import com.fz.service.CartService;
import com.fz.vo.ResultVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 存储购物车中商品信息 前端控制器
 * </p>
 *
 * @author hyr
 * @since 2025-06-18
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Resource
    private CartService cartService;
    @GetMapping("/list")
    ResultVo<IPage<Cart>> list(Long userId, Integer pageNo, Integer pageSize) {
        IPage<Cart> cartPage = cartService.getByUserId(userId, pageNo, pageSize);
        if (cartPage.getRecords().isEmpty()) {
            return ResultVo.success(cartPage); // 返回空的分页结果
        }
        return ResultVo.success(cartPage);
    }
    @PostMapping("/add")
    ResultVo<Boolean> add( Long goodsId,Long userId, Integer count) {
        boolean add=cartService.add( goodsId,userId, count);
        if (add) {
            return ResultVo.success(add, "已加入购物车");
        }
        else {
            return ResultVo.success(add);
        }
    }

    @PostMapping("/del")
    ResultVo<Boolean> del(Long id) {
        return ResultVo.success(cartService.removeById(id));
    }

    @PostMapping("/update")
    ResultVo<Boolean> update(Long id, Integer count) {
        return ResultVo.success(cartService.update(id, count),"修改商品数量成功");
    }
}
